package com.testing.class1;

import com.testing.DriverSelf.FFDriver;
import com.testing.DriverSelf.GoogleDriver;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;

/**
 * @Classname FirstDemo
 * @Description 类型说明
 * @Date 2023/10/10 21:43
 * @Created by 特斯汀Roy
 */
public class FirstDemo {
    public static void main(String[] args) throws InterruptedException {
        //指定webdriver应用程序存放的位置。
        System.setProperty("webdriver.chrome.driver", "E:\\AutoTools\\chromedriver\\117149\\chromedriver-win64\\chromedriver.exe");
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--user-data-dir=C:\\Users\\roy08\\AppData\\Local\\Google\\Chrome\\User Data");
        WebDriver driver = new ChromeDriver(options);
//        GoogleDriver gg=new GoogleDriver("DriverExe\\chromedriver.exe");
//        WebDriver driver=gg.getDriver();

        //火狐
//        FFDriver ff=new FFDriver("","DriverExe\\geckodriver.exe");
//        WebDriver driver=ff.getDriver();
//        System.setProperty("webdriver.gecko.driver","DriverExe\\geckodriver.exe");
//        WebDriver driver=new FirefoxDriver();
//        WebDriver driver=new FirefoxDriver(new FirefoxOptions().setProfile(new FirefoxProfile()));

        //Edge
//        System.setProperty("webdriver.edge.driver","DriverExe\\msedgedriver.exe");
//        WebDriver driver=new EdgeDriver();

//        //IE
//        System.setProperty("webdriver.ie.driver","DriverExe\\IEDriverServer.exe");
//        WebDriver driver=new InternetExplorerDriver();


        //访问网页
        driver.get("https://www.xiaohongshu.com/explore");
//        driver.get("https://www.baidu.com");
        //强制等待3秒
        Thread.sleep(300000);
        driver.navigate().to("https://www.jd.com");
        //浏览器回退
        driver.navigate().back();
        //调整浏览器的左上角位置
        Point point = new Point(293, 22);
        driver.manage().window().setPosition(point);
        //设置大小
        Dimension dimension = new Dimension(1608, 988);
        driver.manage().window().setSize(dimension);

        //定位一个元素，通过css选择器表达式定位
        WebElement element = driver.findElement(By.cssSelector("#kw"));
        element.sendKeys("晓");
        //完成点击
        try {
            driver.findElement(By.id("wd")).click();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //网页还在加载，标题还没来得及变，就获取标题了，脚本执行太快，浏览器没跟上
        //让脚本等一等
        Thread.sleep(1000);
        //断言标题内容
        String title = driver.getTitle();
        System.out.println(title);
        if(title.equals("晓_百度搜索")){
            System.out.println("测试成功");
        }else{
            System.out.println("测试失败");
        }

        try {
            String text = driver.findElement(By.cssSelector(".result-op.c-container.new-pmd:nth-of-type(1) h3 span:nth-child(1) span:nth-child(1)")).getText();
            System.out.println(text);
        } catch (Exception e) {
            System.out.println("测试失败，没有找到该元素");
        }

        //关闭当前窗口
//        driver.close();
        //退出浏览器，关掉chromeDriver
        driver.quit();

    }
}
